#include <pthread.h>
#include <ctime>
#include <cstdlib>
#include "RingQueue.hpp"
#include "Task.hpp"

void* consumer(void* ring_queue)
{
	RingQueue<Task>* rq = (RingQueue<Task>*)ring_queue;
	while(true)
	{
		Task t;
		int c_id = rq->Get(&t);
		int result = t.Handler();
		std::cout << "消费者"<< c_id << "号" << " result :" << result << std::endl;
		usleep(5);
	}
}
void* productor(void* ring_queue)
{
	RingQueue<Task> * rq = (RingQueue<Task>*)ring_queue;
	while(1)
	{
		int top = rand()%20+1;
		Task t(top);
		int p_id = rq->Put(t);
		std::cout << "生产者" << p_id << "号 ";
		t.Show();
		usleep(5);
	}
}
int main()
{
	RingQueue<Task> * rq = new RingQueue<Task>(10);

	pthread_t c,p;
	pthread_create(&c,nullptr,consumer,rq);
	pthread_create(&p,nullptr,productor,rq);

	pthread_join(c,nullptr);
	pthread_join(p,nullptr);
	return 0;
}
